<?php
/**
 * 登录相关业务
 */

namespace app\api\business;
use app\common\lib\Str;
use app\common\lib\Time;
use think\Exception;
use app\common\model\redis\RedisModel;
class LoginBus extends BaseBus
{
    public static $name = 'business.LoginBus';
    /**
     * 登录验证业务
     * @param $data // 用户登录提交数据
     * @return bool
     * @throws Exception
     */
    public static function login($data)
    {
        try{
            $userInfo = self::getUserByUserNumber($data['account']);
            if(empty($userInfo)){
                return returnResultExit(config("config.code.login.non_existent"),config("config.describe.non_existent"));
            }
            //判断密码是否正确
            if($userInfo['password'] != md5($data['password'])){
                return returnResultExit(config("config.code.password_error"),config("config.describe.password_error"));
            }
            // 账户被限制登陆
            if ($userInfo['status'] ==2){
                return returnResultExit(config("config.code.restricted"),config("config.describe.restricted"));
            }
            //验证token
            $RedisModel = new RedisModel();
            $token_id = $RedisModel->getValue(config("redis.token_id").$userInfo['id']);
            $token_pre = $RedisModel->getValue(config("redis.token_pre").$token_id);
            $redisData = json_encode($userInfo);
            if(!$token_pre){
                // 获取token
                $token  = Str::getLoginToken($userInfo['account']);
                // 以用户ID保存用户的唯一token
                $RedisModel->setexValue(config("redis.token_id").$userInfo['id'],$token,Time::userLoginExpiresTime());
                // 保存用户token的相关数据
                $RedisModel->setexValue(config("redis.token_pre").$token,$redisData,Time::userLoginExpiresTime());
            }else{
                $token = $RedisModel->getValue(config("redis.token_id").$userInfo['id']);
                // 以用户ID保存用户的唯一token
                $RedisModel->setexValue(config("redis.token_id").$userInfo['id'],$token,Time::userLoginExpiresTime());
                // 保存用户token的相关数据
                $RedisModel->setexValue(config("redis.token_pre").$token,$redisData,Time::userLoginExpiresTime());
            }
            $userInfo['token'] = $token;
            return $userInfo;
        } catch(\Exception $e) {
            exception($e,'LoginBus.login');
        }
    }

    /**
     * 检查帐号是否存在
     * @param $user_number
     * @return array|false|void
     */
    public static function getUserByUserNumber($user_number)
    {
        try{
            $where = [
                "account" => $user_number,
            ];
            $userInfo = \app\common\model\mysql\Admin::getUserByCriteria($where);
            if(empty($userInfo)){
                return false;
            }
            return $userInfo->toArray();
        } catch(\Exception $e) {
            exception($e,'business.LoginBus.getUserByUserNumber');
        }
    }

    /**
     * 获取token对应的用户信息
     * @param $userId //用户ID
     * @return string|void|null
     */
    public static function getUserInfo($userId)
    {
        try{
            $RedisModel = new RedisModel();
            $token_id = $RedisModel->getValue(config("redis.token_id").$userId);
            $token_pre = $RedisModel->getValue(config("redis.token_pre").$token_id);
            return $token_pre;
        }catch (\Exception $e){
            exception($e,LoginBus::$name.'.getUserInfo');
        }
    }
}
